package com.tac.service.impl;

import com.tac.common.Exception.CostomException;
import com.tac.common.result.Result;
import com.tac.common.thread.Thread;
import com.tac.dao.FootprintDao;
import com.tac.dao.ProductDao;
import com.tac.pojo.Footprint;
import com.tac.pojo.Product;
import com.tac.service.FootprintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FootprintServiceImpl implements FootprintService {
    @Autowired
    private FootprintDao footprintDao;
    @Autowired
    private ProductDao productDao;
    /**
     * 删除收藏
     * @param
     * @return
     */
    public Result remove(Integer pid){
        Integer remove = footprintDao.remove(pid, Thread.getId());
        if(remove>0){
            return Result.success(null,"success");
        }
        throw new CostomException("删除失败");
    }

    /**
     * 查看收藏所有商品 按时间倒序排列
     * @param
     * @return
     */
    public Result<List<Product>> selectByUid(){
        List<Integer> integers = footprintDao.selectByUid(Thread.getId());
        ArrayList<Product> products = new ArrayList<>();
        for(Integer integer:integers){
            Product product = productDao.selectById(integer);
            if(product!=null) {
                product.setCollectStatus(1);
                products.add(product);
            }
        }
        return Result.success(products,"success");
    }

    /**
     * 管理员查看足迹所有商品 按时间倒序排列
     * @param
     * @return
     */
    public Result<List<Footprint>> selectAll(){
        List<Footprint> footprints = footprintDao.selectAll();
        for(Footprint footprint:footprints){
            Product product = productDao.selectById(footprint.getProductId());
            footprint.setProduct(product);
        }
        return Result.success(footprints,"success");
    };

    /**
     * 管理员删除足迹
     * @param pid
     * @param uid
     * @return
     */
    public Result delete(Integer pid,Integer uid){
        Integer remove = footprintDao.remove(pid, uid);
        if(remove>0){
            return Result.success(null,"success");
        }
        throw new CostomException("删除失败");
    }
}
